﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcCarConsume.Domain.Entites;

namespace MvcCarConsume.Domain
{
    public class SqlAdminService : IAdminService
    {
        private string connStr;

        public SqlAdminService(string connectionString)
        {
            connStr = connectionString;
        }

        /// <summary>
        /// List all of cars
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Car> CarsManage()
        {
            List<Car> cars = new List<Car>();
            using (var db = new MvcCarDBEntities(connStr))
            {
                foreach (var car in db.Car)
                    cars.Add(new Car
                    {
                        CarId = car.CarId,
                        CarName = car.CarName,
                        BrandId = car.BrandId,
                        TypeId = car.TypeId,
                        Count = car.Count,
                        Price = car.Price,
                        Description = car.Description,
                    });

            }
            return cars;
        }

        /// <summary>
        /// delete car by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteCar(int id)
        {
            bool stat=false;
            using (var db = new MvcCarDBEntities(connStr))
            {
                var deletecar = db.Car.Single(a => a.BrandId == id);
                try
                {
                    db.Car.DeleteObject(deletecar);
                    db.SaveChanges();
                    stat = true;
                }
                catch
                {
                    stat = false;
                }

            }
            return stat;
        }

        /// <summary>
        /// Add new Car
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public bool AddCar(Car car)
        {
            bool stat=false;
            using (var db = new MvcCarDBEntities(connStr))
            {
                try
                {
                    db.Car.AddObject(car);
                    //db.AddToCar(car);
                    db.SaveChanges();
                    stat = true;
                }
                catch
                {
                    stat = false;
                }

            }

            return stat;
        }

        /// <summary>
        /// Get catinfo by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Car GetCar(int id)
        {
            Car car = new Car();
            using (var db = new MvcCarDBEntities(connStr))
            {
                var newcar = db.Car.Single(a => a.CarId == id);
                if (newcar != null)
                {
                    car.CarId = id;
                    car.CarName = newcar.CarName;
                    car.Count = newcar.Count;
                    car.Description = newcar.Description;
                    car.Brand = newcar.Brand;
                    car.Type = newcar.Type;
                    car.Price = newcar.Price;
                }
            }
            return car;
        }

        /// <summary>
        /// save changes of car
        /// </summary>
        /// <param name="car"></param>
        /// <returns></returns>
        public bool SaveCar(Car car)
        {
            bool stat = false;
            using (var db = new MvcCarDBEntities(connStr))
            {
                try
                {
                    var dbcar = db.Car.Single(a => a.CarId == car.CarId);
                    dbcar.CarName = car.CarName;
                    dbcar.Brand = car.Brand;
                    dbcar.Type = car.Type;
                    dbcar.Count = car.Count;
                    dbcar.Description = car.Description;
                    dbcar.Price = car.Price;
                    db.SaveChanges();
                    stat = true;
                }
                catch
                {
                    stat = false;
                }

            }
            return stat;
        }

        /// <summary>
        /// get all orders
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Order> OrdersManage()
        {
            List<Order> orders = new List<Order>();
            using (var db = new MvcCarDBEntities(connStr))
            {
                foreach (var order in db.Order)
                    orders.Add(new Order
                    {
                        CarId = order.CarId,
                        BorrowDate = order.BorrowDate,
                        SendDate = order.SendDate,
                        State = order.State,
                        Count = order.Count,
                        Price = order.Price,
                        OrderId = order.OrderId,
                        UserName = order.UserName,
                        BuildDate = order.BuildDate
                    });
            }

            return orders;
        }

        /// <summary>
        /// delete order by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteOrder(int id)
        {
            bool stat = false;
            using (var db = new MvcCarDBEntities(connStr))
            {
                var deleteorder = db.Order.Single(a => a.OrderId == id);
                try
                {
                    db.Order.DeleteObject(deleteorder);
                    db.SaveChanges();
                    stat = true;
                }
                catch
                {
                    stat = false;
                }
            }
            return stat;
        }

        /// <summary>
        /// add new order
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public bool AddOrder(Order order)
        {
            bool stat = false;
            using (var db = new MvcCarDBEntities(connStr))
            {
                try
                {
                    db.AddToOrder(order);
                    db.SaveChanges();
                    stat = true;
                }
                catch
                {
                    stat = false;
                }
            }
            return stat;
        }

        /// <summary>
        /// get orderinfo by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Order GetOrder(int id)
        {
            Order order = new Order();
            using (var db = new MvcCarDBEntities(connStr))
            {
                var neworder = db.Order.Single(a => a.OrderId == id);
                if (neworder != null)
                {
                    order.OrderId = id;
                    order.BorrowDate = neworder.BorrowDate;
                    order.Count = neworder.Count;
                    order.SendDate = neworder.SendDate;
                    order.State = neworder.State;
                    order.Price = neworder.Price;
                    order.UserName = neworder.UserName;
                    order.BuildDate = neworder.BuildDate;
                    order.CarId = neworder.CarId;
                }
            }
            return order;
        }

        /// <summary>
        /// Save changes for order
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public bool SaveOrder(Order order)
        {
            bool stat = false;
            using (var db = new MvcCarDBEntities(connStr))
            {
                try
                {
                    var dborder = db.Order.Single(a => a.OrderId == order.OrderId);
                    dborder.BorrowDate = order.BorrowDate;
                    dborder.Count = order.Count;
                    dborder.SendDate = order.SendDate;
                    dborder.State = order.State;
                    dborder.Price = order.Price;
                    dborder.UserName = order.UserName;
                    dborder.BuildDate = order.BuildDate;
                    dborder.CarId = order.CarId;
                    db.SaveChanges();
                    stat = true;
                }
                catch
                {
                    stat = false;
                }

            }
            return stat;
        }
    }
}
